<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2019-11-12
 * Time: 20:13
 */

namespace app\api\service;
use app\api\model\GoodsOrder;
use app\api\model\MoneyOrder;
use app\api\model\StoreOrder;
use app\api\service\order\GoodsOrderSer;
use app\api\service\order\MoneyOrderSer;
use app\api\service\order\StoreOrderSer;


class WxNotify extends \WxPayNotify
{
    public function NotifyProcess($objData, $config, &$msg)
    {
       if($objData['result_code'] == 'SUCCESS' && $objData['return_code'] == 'SUCCESS')
        {

            $outTradeNo = $objData['out_trade_no'];
            //获取通知的数据
            if (empty($outTradeNo)) {
                # 如果没有数据，直接返回失败
                return false;
            }
            /**
             * 根据订单类型进行操作
             */
            $outTradeNoArr = explode("z",$outTradeNo);
            $type = $outTradeNoArr[0];
            if($type == 1) {
                // 余额订单
                return $this->updateMoneyOrder($outTradeNo);
            } elseif ($type == 2) {
                // 门店订单
                return $this->updateStoreOrder($outTradeNo);
            } elseif ($type == 3) {
                // 商品订单
                return $this->updateGoodsOrder($outTradeNo);
            }
        }
        else{
            return true;
        }
    }

    /**
     * 余额订单
     */
    private function updateMoneyOrder($outTradeNo)
    {
        $order = MoneyOrder::where('out_trade_no',$outTradeNo)->find();
        $moneySer = new MoneyOrderSer($order);
        return $moneySer->changeStatus();
    }

    /**
     * 门店订单
     */
    private function updateStoreOrder($outTradeNo)
    {
        $order = StoreOrder::where('out_trade_no',$outTradeNo)->find();
        $StoreOrderSer = new StoreOrderSer($order);
        return $StoreOrderSer->changeStatus();
    }

    /**
     * 商城订单
     */
    private function updateGoodsOrder($outTradeNo)
    {
        $order = GoodsOrder::where('out_trade_no',$outTradeNo)->find();
        $GoodsOrderSer = new GoodsOrderSer($order);
        return $GoodsOrderSer->changeStatus();
    }
}